package com.friend.joey.applicationfriend.Classes;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;

/**
 * Created by Joey on 2017/4/20.
 */

public class JSONRequest {
    private JSONObject requestobj;
    private URL url;

    public JSONRequest(){
        //requestobj = new JSONObject();
    }

    public JSONRequest(JSONObject obj,String requesturl){
        requestobj = obj;
        try {
            url = new URL(requesturl);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public int tryconnect(){
        try {
            Socket s= new Socket("192.168.191.1",8000);
            //System.out.println("socket建立成功");
            return 1;
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            //System.out.println("socekt失败，未知错误");
            return -1;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            //System.out.println("socket失败，IOException");
            return -1;
        }
    }

    public JSONObject jsonRequest(){
        JSONObject resultobj=new JSONObject();
        try {
            //JSONObject obj = new JSONObject();
            //obj.put("text", "威海提醒");
            //URL url=new URL("http://192.168.191.2:8000/jsontest");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setConnectTimeout(6*1000);
            connection.setRequestProperty("Charset", "UTF-8");//设置编码类型
            connection.setRequestProperty("contentType", "application/json");//设置文件类型:
            byte[] data = (requestobj.toString()).getBytes("utf-8");
            connection.setRequestProperty("Content-Length", String.valueOf(data.length));//设置文件长度
            connection.connect();
            OutputStream out = connection.getOutputStream();
            out.write(data);
            if (connection.getResponseCode() == 200) {
                System.out.println("连接成功");
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
                String lines=null;
                StringBuffer sb = new StringBuffer("");
                while ((lines = reader.readLine()) != null) {
                    sb.append(lines);
                }
                resultobj=new JSONObject(sb.toString());
                String answer= resultobj.getString("result");//
                System.out.println(sb);//
                System.out.println(resultobj);//
                System.out.println(answer);//
                reader.close();
            }
            else{
                System.out.println("连接失败");//
            }
            connection.disconnect();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("erro1111");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultobj;
    }

}
